package com.loopy.fruns.service.impl;

import com.loopy.fruns.dao.FurnDAO;
import com.loopy.fruns.dao.impl.FurnDAOImpl;
import com.loopy.fruns.entity.Furn;
import com.loopy.fruns.entity.Page;
import com.loopy.fruns.service.FurnService;

import java.util.List;

public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public int add(Furn furn) {
        return furnDAO.addFurn(furn);
    }

    @Override
    public int deleteFurnById(Integer id) {
        return furnDAO.deleteFurnById(id);
    }

    @Override
    public Furn queryFurnById(Integer id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        Page<Furn> page = new Page<>();
//        return furnDAO.getPageItem(begin, pageSize);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRow();
        page.setPageRow(totalRow);
        int totalPageCount = totalRow / pageSize;
        if (totalRow % pageSize > 0) {
            totalPageCount+=1;
        }
        page.setPageToatlCount(totalPageCount);
        int begin=(pageNo-1)*pageSize;
        List<Furn> pageItem = furnDAO.getPageItem(begin, pageSize);
        page.setItems(pageItem);
        return page;
    }

    @Override
    public Page<Furn> pageByName(String name, int pageNo, int pageSize) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRowByName = furnDAO.getTotalRowByName(name);
        page.setPageRow(totalRowByName);
        int pageTotalCount=totalRowByName/pageSize;
        if(totalRowByName%pageSize>0){
            pageTotalCount+=1;
        }
        page.setPageToatlCount(pageTotalCount);
        int begin=(pageNo-1)*pageSize;
        page.setItems(furnDAO.getPageItemByName(name,begin,pageSize));
        return  page;
    }

}
